package com.exmaple.springcloud.controller;

import cn.hutool.core.util.StrUtil;
import com.exmaple.springcloud.domain.R;
import com.exmaple.springcloud.entity.PayDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {
    //public static final String PaymentSrv_URL="http://localhost:8001";
    public static final String PaymentSrv_URL = "http://cloud-payment-service";//服务注册中心上的微服务名称

    @Autowired
    private RestTemplate restTemplate;


    @PostMapping("/consumer/pay/add")
    public R addOrder(@RequestBody  PayDTO payDTO){
        return restTemplate.postForObject(PaymentSrv_URL+"/pay/add",payDTO,R.class);
    }


    @GetMapping("/consumer/pay/get/{id}")
    public R getPayInfo(@PathVariable("id") Integer id){
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/"+id, R.class, id);
    }


    @GetMapping("/consumer/pay/getEntity/{id}")
    public R<PayDTO> getPayInfo2(@PathVariable("id") Integer id){
        ResponseEntity<R> entity = restTemplate.getForEntity(PaymentSrv_URL + "/pay/get/" + id, R.class);
        if (entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }else {
            return R.fail("88","操作失败");

        }
    }

    @GetMapping(value = "/consumer/pay/get/info")
    private String getInfoByConsul(){
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", String.class);
    }


}
